package cn.wolfcode.user.servlet;

/*
 * @author 李新雨的项目86176
 * @version 1.0
 * @date 2024-11-06 15:02
 */

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

@WebServlet("/randomCode")
public class RandomCodeServlet extends HttpServlet {

    // 验证码图片的宽度
    private static final int WIDTH = 100;
    // 验证码图片的高度
    private static final int HEIGHT = 40;
    // 验证码的字符数
    private static final int CODE_LENGTH = 4;
    // 验证码的字符集
    private static final String CHAR_SET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    private static BufferedImage image;
    private static String verificationCode;

//    public static void main(String[] args) {
//        // 生成验证码图片并保存到文件
//        try {
//            BufferedImage image = image;
//            File output = new File("verification_code.png");
//            ImageIO.write(image, "png", output);
//            System.out.println("验证码图片已生成并保存到: " + output.getAbsolutePath());
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    // 创建一个空白的图片
        BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = image.createGraphics();

        // 设置背景色为白色
        g2d.setColor(Color.WHITE);
        g2d.fillRect(0, 0, WIDTH, HEIGHT);

        // 随机生成验证码字符
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < CODE_LENGTH; i++) {
            int index = random.nextInt(CHAR_SET.length());
            char c = CHAR_SET.charAt(index);
            sb.append(c);
        }
        String verificationCode = sb.toString();
        RandomCodeServlet.verificationCode =verificationCode;
        req.getSession().setAttribute("RANDOMCODE_IN_SESSION",verificationCode);
        System.out.println(verificationCode);

        // 设置字体和颜色
        g2d.setFont(new Font("Arial", Font.BOLD, 30));
        g2d.setColor(Color.BLUE);

        // 绘制验证码字符到图片上
        for (int i = 0; i < CODE_LENGTH; i++) {
            g2d.drawString(String.valueOf(verificationCode.charAt(i)), 20 * i + 10, 30);
        }

        // 添加随机干扰线
        g2d.setColor(Color.GRAY);
        for (int i = 0; i < 10; i++) {
            int x1 = random.nextInt(WIDTH);
            int y1 = random.nextInt(HEIGHT);
            int x2 = random.nextInt(WIDTH);
            int y2 = random.nextInt(HEIGHT);
            g2d.drawLine(x1, y1, x2, y2);
        }

        // 释放资源
        g2d.dispose();

        // 返回生成的图片
        RandomCodeServlet.image =image;
//        String path=req.getServletContext().getRealPath("image/");
        File output = new File("verification_code.png");
//        String path=req.getServletContext().getRealPath("suiJiCode/");
        ImageIO.write(image,"png",resp.getOutputStream());
        System.out.println("验证码图片已生成并保存到: " + output.getAbsolutePath());
    }

    public  BufferedImage getImage() {
        return image;
    }

    public  String getVerificationCode() {
        return verificationCode;
    }
}